package com.gonsin.gview.logic.services;

import com.gonsin.gview.logic.config.GViewGlobalEnumsService;
import com.gonsin.gview.logic.exceptions.BaseGViewException;
import com.gonsin.gview.model.struct.EnumItem;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Service
public class GViewGlobalEnumsServiceImpl implements GViewGlobalEnumsService {

    private Map<String, List<EnumItem>> enumsMap = new ConcurrentHashMap<>();

    @Override
    public void addEnums(String enumName, List<EnumItem> enums) {
        if(StringUtils.isEmpty(enumName)){
            return;
        }
        if(enums == null){
            return;
        }
        enumsMap.put(enumName, enums);
    }

    @Override
    public List<EnumItem> getEnums(String enumName) {
        if(!enumsMap.containsKey(enumName)){
            throw new BaseGViewException(String.format("无法查找到【%s】的枚举名称，请使用  GViewGlobalEnumsService.addEnums 方法添加枚举", enumName));
        }
        return new ArrayList<>(enumsMap.get(enumName));
    }
}
